package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import javax.swing.*;
import java.awt.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class InputDialog {
    private static boolean initDialog(StringBuilder name, StringBuilder alias, StringBuilder description, String title) {
        JPanel inputPanel = new JPanel(new GridLayout(3, 2));
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());

        String nameLabel = resourceBundle.getString("inputdialog.name");
        String aliasLabel = resourceBundle.getString("inputdialog.alias");
        String descriptionLabel = resourceBundle.getString("inputdialog.description");

        JTextField nameField = new JTextField();
        JTextField aliasField = new JTextField();
        JTextField descriptionField = new JTextField();

        inputPanel.add(new JLabel(nameLabel));
        inputPanel.add(nameField);
        inputPanel.add(new JLabel(aliasLabel));
        inputPanel.add(aliasField);
        inputPanel.add(new JLabel(descriptionLabel));
        inputPanel.add(descriptionField);
        int result = JOptionPane.showConfirmDialog(null, inputPanel, title, JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            if (nameField.getText().isEmpty() || aliasField.getText().isEmpty())
                return false;
            name.append(nameField.getText());
            alias.append(aliasField.getText());
            description.append(descriptionField.getText());
            return true;
        }
        return false;
    }

    public static boolean showInputCatalogDialog(StringBuilder name, StringBuilder alias, StringBuilder description) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());
        String title = resourceBundle.getString("inputdialog.entercataloginformation");
        return initDialog(name, alias, description, title);
    }

    public static boolean showInputGroupDialog(StringBuilder name, StringBuilder alias, StringBuilder description) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());
        String title = resourceBundle.getString("inputdialog.entergroupinformation");
        return initDialog(name, alias, description, title);
    }
}
